<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <!--
        登录表单
        el-form > el-form-item > el-input
        -->
      <el-form ref="form" :model="formData" :rules="rules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" show-password />
        </el-form-item>
        <el-form-item prop="remember">
          <el-checkbox v-model="formData.remember">记住我</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login_btn" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { LOGIN_FORMDATA_KEY } from '@/constants/KEY'

export default {
  name: 'Login',
  data() {
    return {
      formData: {
        username: '',
        password: '',
        remember: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 如果之前选择过记住我，那么用户再进入登录页面的时候记住我保持选中状态
    if (localStorage.getItem(LOGIN_FORMDATA_KEY)) {
      this.formData.remember = true
    }
  },
  mounted() {
    // 实现 登录表单记住我 功能
    const formDataCacheStr = localStorage.getItem(LOGIN_FORMDATA_KEY)
    if (formDataCacheStr) {
      const formDataObj = JSON.parse(formDataCacheStr)
      this.formData.username = formDataObj.username
      this.formData.password = formDataObj.password
    }
  },
  methods: {
    async login() {
      // 校验表单
      await this.$refs.form.validate()
      // 登录请求
      const { username, password } = this.formData
      await this.$store.dispatch('user/login', { username, password })
      // 实现记住我逻辑
      if (this.formData.remember) {
        // 如果勾选了记住我，就将账号和密码持久化
        localStorage.setItem(LOGIN_FORMDATA_KEY, JSON.stringify({ username, password }))
      } else {
        localStorage.removeItem(LOGIN_FORMDATA_KEY)
      }
      // 登录成功，跳转首页
      this.$router.push('/')
      this.$message.success('登录成功~~')
    }
  }
}
</script>

<style scoped lang="scss">
.login_body {
  display: flex;
}

.bg {
  width: 60vw;
  height: 100vh;
  background: url('~@/assets/login-bg.svg') no-repeat;
  background-position: right top;
  background-size: cover;
}

.box {
  margin: 200px 10% 0;
  flex: 1;

  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }

  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }

    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }

    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}

.login_btn {
  width: 100%;
}
</style>
